<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>

<body>

    <h1>快速排序</h1>

    <script>
        function quickSort (arr) {
            function qs(data) {
                if (data.length <=1 ) return data;
                let mid = Math.floor(data.length/2);
                let midVal = data.splice(mid, 1)[0];
                let left = [];
                let right = [];

                for (let i=0, len=data.length; i<len; i++) {
                    if (data[i] < midVal) {
                        left.push(data[i]);
                    }
                    else {
                        right.push(data[i]);
                    }
                }

                return qs(left).concat(midVal, qs(right));
            }
            return qs(arr);
        }
        
        let res = quickSort([546,22,44,-45,52,24,54]);
        console.log(res);
    </script>
</body>

</html>